package struts;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

public class XmalMap {

	public XmalMap(){
		
	}
	
	//解析XML
	public static Map<String,ConfigBean> parsXml(String xmlpath){
		Map<String,ConfigBean> map = new HashMap<String, ConfigBean>();
		Document doc = XMLUtil.read(xmlpath);
		List<Node> nodes = doc.selectNodes("/struts/actionmapping/action");
		if(nodes != null && nodes.size() > 0){
			//循环所有ACTION
			for(Node node : nodes){
				ConfigBean bean = new ConfigBean();
				Element elt = (Element)node;
				bean.setActionClass(elt.attribute("type").getValue());  
				bean.setFormname(elt.attribute("name").getValue());
				bean.setPath(elt.attribute("path").getValue());
				//找出from对象
				List<Node> fromnodes = doc.selectNodes("/struts/formbeans/bean");
				for(Node fromnode : fromnodes){
					Element fromelt = (Element)fromnode;
					if(fromelt.attribute("name").getValue().equals(bean.getFormname())){
						bean.setFormClass(fromelt.attribute("class").getValue());
						break;
					}
				}
				//取出转发对象List
				List<Node> forwarList = node.selectNodes("forward");
				Map<String , String> forwars = new HashMap<String, String>();
				for(Node forward : forwarList){
					Element forwardElt = (Element)forward;
					forwars.put(forwardElt.attribute("name").getValue(), forwardElt.attribute("value").getValue());
				}
				bean.setForwardList(forwars);
				map.put(bean.getPath(), bean);
			}
		}
		return map;
	}
	
	public static void main(String args[]){
		new XmalMap().parsXml("c:\\struts-config.xml");
	}
}
